package com.ocom.wxapi.controller;

import com.ocom.common.entity.emqx.EntityYhMqtt0606;
import com.ocom.common.entity.emqx.EntityYhMqtt0607;
import com.ocom.common.enums.ResultEnum;
import com.ocom.common.request.emqx.YhMqtt0606Request;
import com.ocom.common.request.emqx.YhMqtt0607Request;
import com.ocom.common.request.payconfig.IotDeviceListRequest;
import com.ocom.common.request.payconfig.NewIotDeviceRequest;
import com.ocom.common.request.payconfig.RemoveIotDeviceRequest;
import com.ocom.common.request.payconfig.UpdateIotDeviceRequest;
import com.ocom.common.resp.Result;
import com.ocom.common.utils.ResultUtil;
import com.ocom.security.authentication.YoCiUser;
import com.ocom.security.utils.SecurityUtils;
import com.ocom.wxapi.client.EmqxClientService;
import com.ocom.wxapi.client.PayConfigClientService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.math.BigDecimal;

@RestController
public class IotDeviceController {
    @Autowired
    private PayConfigClientService payConfigClientService;

    @Autowired
    private EmqxClientService emqxClientService;

    @PostMapping( "/IotDevice/onShow")
    public Result OnShow(IotDeviceListRequest request){
//        YoCiUser yoCiUser = SecurityUtils.getYoCiUser();
//        request.setMerchId(yoCiUser.getMerchId());
//        request.setComId(yoCiUser.getComId());
        return this.payConfigClientService.iotDeviceList(request);
    }

    @PostMapping("/IotDevice/add")
    public Result Add(NewIotDeviceRequest request){
//        YoCiUser yoCiUser = SecurityUtils.getYoCiUser();
//        request.setMerchId(yoCiUser.getMerchId());
//        request.setComId(yoCiUser.getComId());
        return this.payConfigClientService.newIotDevice(request);
    }
    @PostMapping("/IotDevice/modify")
    public Result Modify( UpdateIotDeviceRequest request){
//        YoCiUser yoCiUser = SecurityUtils.getYoCiUser();
//        request.setMerchId(yoCiUser.getMerchId());
//        request.setComId(yoCiUser.getComId());
        return this.payConfigClientService.updateIotDevice(request);
    }
    @PostMapping("/IotDevice/delete")
    public Result Delete(RemoveIotDeviceRequest request){
//        YoCiUser yoCiUser = SecurityUtils.getYoCiUser();
//        request.setMerchId(yoCiUser.getMerchId());
//        request.setComId(yoCiUser.getComId());
        return this.payConfigClientService.removeIotDevice(request);
    }

    //洗衣机售后调试测试
    @PostMapping("/yh0607")
    public Result yhMqtt0607(@Valid YhMqtt0607Request request){
        request.setOrderNo(0L);
        Result<EntityYhMqtt0607> resultA = this.emqxClientService.yhMqtt0607(request);
        if(resultA.getCode().equals(200)){
            return ResultUtil.success();
        }
        return ResultUtil.error(ResultEnum.Err_6006);
    }

    //吹风机测试
    @PostMapping("/yh0606")
    public Result yhMqtt0606( YhMqtt0606Request request){
        request.setOrderNo(0L);
        request.setTotalTime(1);
        request.setOverTime(BigDecimal.ONE);
        Result<EntityYhMqtt0606> resultA = this.emqxClientService.yhMqtt0606(request);
        if(resultA.getCode().equals(200)){
            return ResultUtil.success("请一分钟内按下吹风机电源键开关使用");
        }
        return ResultUtil.error(ResultEnum.Err_6006);
    }
}
